package com.tywj.buscustomerapp.view.myview;

import android.animation.AnimatorSet;
import android.animation.ObjectAnimator;
import android.content.Context;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v7.widget.CardView;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import android.view.animation.AnimationUtils;
import android.view.animation.ScaleAnimation;

import com.tywj.buscustomerapp.R;
import com.tywj.buscustomerapp.utils.SpringScaleInterpolator;

public class TaylorCardView extends CardView{
    private Context context;
    private boolean isS = false;
    private OnClickLis onClickLis;
    public  interface OnClickLis{
        void onClicked();
    }
    public TaylorCardView(@NonNull Context context) {
        super(context);
        this.context = context;
    }

    public TaylorCardView(@NonNull Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        this.context = context;
    }

    public TaylorCardView(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        this.context = context;
    }

    public void setOnclickListen(OnClickLis onClickLis){
        this.onClickLis = onClickLis;
    }



    @Override
    public boolean onTouchEvent(MotionEvent event) {
        switch (event.getAction()){
            case MotionEvent.ACTION_DOWN:
                if (!isS){
                    changeS();
                }
                break;
            case MotionEvent.ACTION_UP:
                if (isS){
                    changeBig(true);
                }
                break;
            case MotionEvent.ACTION_MOVE:
                if (isS){
                    changeBig(false);
                }
                break;
            case MotionEvent.ACTION_CANCEL:
                if (isS){
                    changeBig(false);
                }
                break;
            case MotionEvent.ACTION_HOVER_MOVE:
                if (isS){
                    changeBig(false);
                }
                break;
        }
        return true;
    }

    private void changeS(){
        ObjectAnimator animatorX = ObjectAnimator.ofFloat(this, "scaleX", 1.0f, 0.95f);
        ObjectAnimator animatorY = ObjectAnimator.ofFloat(this, "scaleY", 1.0f, 0.95f);
        AnimatorSet set = new AnimatorSet();
        set.setDuration(500);
        set.setInterpolator(new SpringScaleInterpolator(0.95f));
        set.playTogether(animatorX, animatorY);
        set.start();
        isS = true;
    }

    private void changeBig(boolean isClick){
        ObjectAnimator animatorX2 = ObjectAnimator.ofFloat(this, "scaleX", 0.95f, 1.0f);
        ObjectAnimator animatorY2 = ObjectAnimator.ofFloat(this, "scaleY", 0.95f, 1.0f);
        AnimatorSet set2 = new AnimatorSet();
        set2.setDuration(500);
        set2.setInterpolator(new SpringScaleInterpolator(0.95f));
        set2.playTogether(animatorX2, animatorY2);
        set2.start();
        if (onClickLis!=null && isClick) {
            onClickLis.onClicked();
        }
        isS = false;
    }
}
